﻿#pragma once

#include "../Utils/HCNMath.h"
#include "../Utils/Utils.h"

#include <fast_io/fast_io_dsal/vector.h>

#include "../SDK/CMinecraftUIRenderContext.h"
#include "../SDK/CEntity.h"
#include "../SDK/CClientInstance.h"
#include "../SDK/Tessellator.h"
#include "../SDK/MatrixStack.h"

enum class Fonts {
	DEFAULT,
	UNICOD,
	SMOOTH,
	RUNE,
	MC
};

struct MaterialPtr {
	char padding[0x138]{};
};

class DrawUtils {
	using mce__VertexFormat__disableHalfFloats_t = void(__fastcall*)(__int64, int, int);
	using Tessellator__initializeFormat_t = void(__fastcall*)(__int64, __int64);
	using tess_vertex_t = void(__fastcall*)(Tessellator* _this, float v1, float v2, float v3);
	using meshHelper_renderImm_t = void(__fastcall*)(int64_t, Tessellator* tesselator, MaterialPtr*);

private:
	static mce__VertexFormat__disableHalfFloats_t mce__VertexFormat__disableHalfFloats;
	static Tessellator__initializeFormat_t Tessellator__initializeFormat;

	static C_MinecraftUIRenderContext* renderCtx;
	static uintptr_t screenContext2d;
	static uintptr_t game3dContext;
	static Tessellator* tesselator;
	static float* colorHolder;
	static glmatrixf refdef;
	static vec2_t fov;
	static vec2_t screenSize;
	static vec3_t origin;
	static float lerpT;
	static MaterialPtr* uiMaterial;
	static MaterialPtr* entityFlatStaticMaterial;

	static MC_Color last_color;

	static tess_vertex_t tess_vertex;
	static meshHelper_renderImm_t meshHelper_renderImm;

public:
	static void init();
	static void setCtx(C_MinecraftUIRenderContext* ctx, const C_GuiData* guiData);
	static void setGameRenderContext(uintptr_t ctx);
	static void flush();
	static void setColor(float r, float g, float b, float a);  // rgba, values from 0 to 1
	static inline void tess__begin(Tessellator* tesselator, int vertexFormat = 3, int numVerticesReserved = 0);
	static C_Font* getFont(Fonts font);
	static Tessellator* get3dTessellator();
	static uintptr_t getScreenContext();
	static MatrixStack* getMatrixStack();
	static float getTextWidth(std::u8string_view textStr, float textSize = 1, Fonts font = Fonts::SMOOTH);
	static float getFontHeight(float textSize = 1, Fonts font = Fonts::SMOOTH);
	static void drawLine(vec2_t start, vec2_t end, float lineWidth) noexcept;
};